// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   OrganizeServiceImp.java

package com.iphaeton.lms.business.organize;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.organize.*;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.util.*;
import org.apache.log4j.Logger;
import org.dom4j.*;

// Referenced classes of package com.iphaeton.lms.business.organize:
//            IOrganizeService

public class OrganizeServiceImp extends PersistentServiceSupport
    implements IOrganizeService
{

    public OrganizeServiceImp()
    {
    }

    public Organize updateOrganize(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function updateOrganize(Organize org)!");
        Organize organize = null;
        try
        {
            organize = (Organize)hbpservice.store(org);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't update organize '" + organize.getName() + "'!");
        }
        return organize;
    }

    public Organize createOrganize(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function createOrganize(Organize org)!");
        Organize organize = null;
        try
        {
            organize = (Organize)hbpservice.store(org);
            Group group = new Group();
            group.setName(org.getName());
            group.setOrganizeId(org.getId());
            group.setType('m');
            group.setComment("\u7EC4\u7EC7:" + org.getName() + "\u7684\u7BA1\u7406\u7528\u6237\u7EC4");
            group = (Group)hbpservice.store(group);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't create organize '" + organize.getName() + "'!");
        }
        return organize;
    }

    public void remove(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function remove(String id)!");
        try
        {
            Organize org = findOrganizeById(id);
            List list = findGroup(org, ' ');
            for(int i = 0; i < list.size(); i++)
            {
                Group g = (Group)list.get(i);
                hbpservice.remove(g);
            }

            hbpservice.remove(org);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove organize by id:'" + id + "'!");
        }
    }

    public void remove(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null Organize isn't allowed in function remove(Organize org)!");
        try
        {
            List list = findGroup(org, ' ');
            for(int i = 0; i < list.size(); i++)
            {
                Group g = (Group)list.get(i);
                hbpservice.remove(g);
            }

            hbpservice.remove(org);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove organize :'" + org.getName() + "'!");
        }
    }

    public Organize findOrganizeById(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function findOrganizeById(String id)!");
        Organize organize = null;
        try
        {
            organize = (Organize)hbpservice.get(id, com.iphaeton.lms.pojo.organize.Organize.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find organize by id:'" + id + "'!");
        }
        return organize;
    }

    public Organize findOrganizeByUrl(String url)
        throws BusinessException
    {
        Organize organize = null;
        String hql = "from Organize o where o.url='" + url + "'";
        try
        {
            List list = hbpservice.query(hql);
            if(list != null && list.size() == 1)
                organize = (Organize)list.get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636E\u7EC4\u7EC7\u5168\u540D\u79F0\u67E5\u8BE2\u7EC4\u7EC7\u673A\u6784\u8282\u70B9\u51FA\u9519!");
        }
        return organize;
    }

    public List findChildOrganize(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function findChildOrganie(Organize org)!");
        List organize = null;
        String hql = "from Organize org where org.parentId='" + org.getId() + "' order by org.type,org.name";
        try
        {
            organize = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find Child Organize by organize:'" + org.getName() + "'!");
        }
        return organize;
    }

    public List findAllChildOrganize(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function findAllChildOrganize(Organize org)!");
        List organize = null;
        String hql = "from Organize org where org.url like '" + org.getUrl() + "%' order by org.url";
        try
        {
            organize = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find Child Organize by organize:'" + org.getName() + "'!");
        }
        return organize;
    }

    public Pagination findChildOrganizePagination(String queryType, String row, int pageSize)
        throws BusinessException
    {
        if(queryType == null)
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "select count(org) from Organize org where org.parentId='" + queryType + "'";
        String hqlQueryStr = "from Organize org where org.parentId='" + queryType + "' and org.parentId!=org.id";
        if(!"".equals(row) && row != null)
            hqlQueryStr = hqlQueryStr + " order by org." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by org.type,org.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find Child Organize!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findPageList(Pagination pageInfo)
    {
        List list = null;
        try
        {
            list = getSpecifiedItems(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public Pagination findGroupPagination(String queryType, char type, String row, int pageSize)
        throws BusinessException
    {
        if(queryType == null || type == 'n')
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "select count(g) from Group g where g.organizeId='" + queryType + "' and g.type='" + type + "'";
        String hqlQueryStr = "from Group g where g.organizeId='" + queryType + "' and g.type='" + type + "'";
        if(!"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by g." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by g.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find group by " + queryType + "!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Organize findRoot()
        throws BusinessException
    {
        Organize root = null;
        String hql = "from Organize o where o.parentId=o.id";
        try
        {
            List list = hbpservice.query(hql);
            if(list != null && list.size() > 0)
                root = (Organize)list.get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find root organize !");
        }
        return root;
    }

    public List findGroup(Organize org, char type)
        throws BusinessException
    {
        List list = null;
        String hql = "from Group g where g.type='" + type + "'";
        if(type == 'u' || type == 'm')
            hql = hql + " and g.organizeId='" + org.getId() + "'";
        hql = hql + " order by g.name";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find group by organize:'" + org.getName() + "'!");
        }
        return list;
    }

    public List findTrain(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function findTrain(Organize org)!");
        List train = null;
        String hql = "from Train t where t.organizeId='" + org.getId() + "' order by t.start desc";
        try
        {
            train = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find train by organize:'" + org.getName() + "'!");
        }
        return train;
    }

    public Pagination findUserPagination(String queryType, String state, String row, int pageSize)
        throws BusinessException
    {
        if(queryType == null)
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "";
        String hqlQueryStr = "";
        if(!"".equals(state))
        {
            selectHql = "select count(u) from User u where u.organizeId='" + queryType + "' and u.state='" + state + "'";
            hqlQueryStr = "from User u where u.organizeId='" + queryType + "' and u.state='" + state + "'";
        } else
        {
            selectHql = "select count(u) from User u where u.organizeId='" + queryType + "'";
            hqlQueryStr = "from User u where u.organizeId='" + queryType + "'";
        }
        if("".equals(row))
            hqlQueryStr = hqlQueryStr + " order by u.name";
        else
            hqlQueryStr = hqlQueryStr + " order by u." + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find User from organize: " + queryType + "!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination findAllUserPagination(String queryType, String state, String row, int pageSize)
        throws BusinessException
    {
        if(queryType == null)
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "";
        String hqlQueryStr = "";
        if(!"".equals(state) && state != null)
        {
            selectHql = "select count(u) from User u where u.organize.url like '" + queryType + "%' and u.state='" + state + "'";
            hqlQueryStr = "from User u where u.organize.url like '" + queryType + "%' and u.state='" + state + "'";
        } else
        {
            selectHql = "select count(u) from User u where u.organize.url like '" + queryType + "%'";
            hqlQueryStr = "from User u where u.organize.url like '" + queryType + "%'";
        }
        if("".equals(row))
            hqlQueryStr = hqlQueryStr + " order by u.name";
        else
            hqlQueryStr = hqlQueryStr + " order by u." + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find User from organize: " + queryType + "!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findUser(Organize org, char state)
        throws BusinessException
    {
        if(org == null || state == 'n')
            throw new BusinessException("null value isn't allowed in function findUser(Organize org, char state)!");
        List user = new LinkedList();
        List list = null;
        String hql = "from User u where u.organizeId='" + org.getId() + "' order by u.name";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find user by organize:'" + org.getName() + "'!");
        }
        for(Iterator it = list.iterator(); it.hasNext();)
        {
            User u = (User)it.next();
            if(state == u.getState() || state == 'n' || state == ' ')
                user.add(u);
        }

        return user;
    }

    public List findAllUser(Organize org, char state)
        throws BusinessException
    {
        if(org == null || state == 'n')
            throw new BusinessException("null value isn't allowed in function findAllUser(Organize org, char state)!");
        List user = new LinkedList();
        user = findUser(org, state);
        List cOrg = findChildOrganize(org);
        for(Iterator it = cOrg.iterator(); it.hasNext();)
        {
            Organize o = (Organize)it.next();
            if(!isRoot(o))
                user.addAll(findAllUser(o, state));
        }

        return user;
    }

    public boolean isLeaf(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function isLeaf(Organize org)!");
        boolean leaf = false;
        List list = findChildOrganize(org);
        if(!isRoot(org))
        {
            if(list == null || list.size() < 1)
                leaf = true;
        } else
        if(list.size() > 1)
            leaf = false;
        else
            leaf = true;
        return leaf;
    }

    public boolean isRoot(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function isRoot(Organize org)!");
        boolean root = false;
        if(org.getParentOrganize().equals(org))
            root = true;
        return root;
    }

    public boolean hasGroup(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function hasGroup(Organize org)!");
        boolean group = false;
        String sql = "select organize_id from t_group where organize_id='" + org.getId() + "' and type not in('m','g','z')";
        List list = null;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u7EC4\u7EC7" + org.getName() + "\u7684\u7528\u6237\u7EC4\u5931\u8D25!");
        }
        if(list != null && list.size() > 0)
            group = true;
        return group;
    }

    public boolean hasTrain(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function hasTrain(Organize org)!");
        boolean train = false;
        List list = findTrain(org);
        if(list != null && list.size() > 0)
            train = true;
        return train;
    }

    public boolean hasUser(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function hasUser(Organize org)!");
        boolean user = false;
        List list = findUser(org, ' ');
        if(list != null && list.size() > 0)
            user = true;
        return user;
    }

    private Element addOrganizeNode(String webRoot, Element parent, Organize org, String type)
    {
        Element child = parent.addElement("tree");
        child.addAttribute("text", org.getName());
        String id = org.getId();
        if("organize".equals(type))
        {
            try
            {
                if(!isLeaf(org))
                {
                    child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getTree&type=organize&id=" + id);
                    child.addAttribute("icon", webRoot + "/images/xtree/organize.gif");
                } else
                {
                    child.addAttribute("icon", webRoot + "/images/xtree/organize1.gif");
                }
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/organizeTxAction.do?method=selectOrganize&id=" + id);
            child.addAttribute("openIcon", webRoot + "/images/xtree/organize2.gif");
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "true");
        }
        if("u".equals(type))
        {
            try
            {
                if(!isLeaf(org) || hasGroup(org))
                    child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getTree&type=u&id=" + id);
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/organizeTxAction.do?method=selectGroup&id=" + id + "&type=u");
            child.addAttribute("icon", webRoot + "/images/xtree/group.gif");
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "false");
        }
        if("user".equals(type))
        {
            try
            {
                if(!isLeaf(org))
                    child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getTree&type=user&id=" + id);
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/organizeTxAction.do?method=selectUser&id=" + id + "&state=a");
            child.addAttribute("icon", webRoot + "/images/xtree/user.gif");
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "false");
        }
        if("move".equals(type) || "od".equals(type) || "op".equals(type))
        {
            try
            {
                if(!isLeaf(org))
                {
                    child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getTree&type=" + type + "&id=" + id);
                    child.addAttribute("icon", webRoot + "/images/xtree/organize.gif");
                } else
                {
                    child.addAttribute("icon", webRoot + "/images/xtree/organize1.gif");
                }
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/organizeTxAction.do?method=userMovePrepare&id=" + id + "&type=" + type);
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "false");
        }
        if("course".equals(type))
        {
            try
            {
                if(!isLeaf(org))
                {
                    child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getTree&type=course&id=" + id);
                    child.addAttribute("icon", webRoot + "/images/xtree/course1.gif");
                } else
                {
                    child.addAttribute("icon", webRoot + "/images/xtree/course.gif");
                }
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
            }
            child.addAttribute("action", webRoot + "/courseTxAction.do?method=selectCourse&id=" + id);
            child.addAttribute("target", "operateFrame");
            child.addAttribute("expended", "false");
        }
        return child;
    }

    private Element addUserSelectOrganizeNode(String webRoot, Element parent, Organize org)
    {
        Element child = parent.addElement("tree");
        child.addAttribute("text", org.getName());
        String id = org.getId();
        try
        {
            if(!isLeaf(org))
                child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getUserSelectTree&id=" + id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        child.addAttribute("action", webRoot + "/roleTxAction.do?method=getUserList&id=" + id);
        child.addAttribute("icon", "");
        child.addAttribute("openIcon", "");
        child.addAttribute("target", "list");
        child.addAttribute("expended", "false");
        return child;
    }

    private Element addOrganizeSelectOrganizeNode(String webRoot, Element parent, Organize org)
    {
        Element child = parent.addElement("tree");
        child.addAttribute("text", org.getName());
        String id = org.getId();
        try
        {
            if(!isLeaf(org))
                child.addAttribute("src", webRoot + "/organizeTxAction.do?method=getOrganizeSelectTree&id=" + id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        child.addAttribute("action", "javascript:selectOrganize('" + org.getId() + "','" + org.getName() + "')");
        child.addAttribute("icon", "");
        child.addAttribute("openIcon", "");
        child.addAttribute("expended", "false");
        return child;
    }

    private Element addGroupNode(String webRoot, Element parent, Group group)
    {
        Element child = parent.addElement("tree");
        child.addAttribute("text", group.getName());
        String id = group.getId();
        child.addAttribute("action", webRoot + "/groupTxAction.do?method=selectUser&id=" + id);
        child.addAttribute("icon", webRoot + "/images/group.gif");
        child.addAttribute("target", "operateFrame");
        child.addAttribute("expended", "false");
        return child;
    }

    public Document getTreeRoot(String webRoot, Organize org, String type)
        throws BusinessException
    {
        if(org == null)
        {
            throw new BusinessException("null org isn't allowed in function getOrganizeTree(Organize org)!");
        } else
        {
            Document doc = DocumentHelper.createDocument();
            Element element = doc.addElement("tree");
            addOrganizeNode(webRoot, element, org, type);
            return doc;
        }
    }

    public Document getUserSelectTreeRoot(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
        {
            throw new BusinessException("null org isn't allowed in function getOrganizeTree(Organize org)!");
        } else
        {
            Document doc = DocumentHelper.createDocument();
            Element element = doc.addElement("tree");
            addUserSelectOrganizeNode(webRoot, element, org);
            return doc;
        }
    }

    public Document getOrganizeSelectTreeRoot(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
        {
            throw new BusinessException("null org isn't allowed in function getOrganizeTree(Organize org)!");
        } else
        {
            Document doc = DocumentHelper.createDocument();
            Element element = doc.addElement("tree");
            addOrganizeSelectOrganizeNode(webRoot, element, org);
            return doc;
        }
    }

    public Document getOrganizeTree(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getOrganizeTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addOrganizeNode(webRoot, element, organize, "organize");
            }

        }
        return doc;
    }

    public Document getUserSelectOrganizeTree(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getUserSelectOrganizeTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addUserSelectOrganizeNode(webRoot, element, organize);
            }

        }
        return doc;
    }

    public Document getOrganizeSelectOrganizeTree(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getUserSelectOrganizeTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addOrganizeSelectOrganizeNode(webRoot, element, organize);
            }

        }
        return doc;
    }

    public Document getGroupTree(String webRoot, Organize org)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addOrganizeNode(webRoot, element, organize, "u");
            }

        }
        List group = findGroup(org, 'u');
        Group g;
        for(Iterator gIt = group.iterator(); gIt.hasNext(); addGroupNode(webRoot, element, g))
            g = (Group)gIt.next();

        return doc;
    }

    public Document getTrainTree(Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getTrainTree(Organize org)!");
        else
            return null;
    }

    public Document getUserTree(String webRoot, Organize org, String type)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getUserTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addOrganizeNode(webRoot, element, organize, type);
            }

        }
        return doc;
    }

    public Document getCourseTree(String webRoot, Organize org)
        throws BusinessException
    {
        if(org == null)
            throw new BusinessException("null org isn't allowed in function getCourseTree(Organize org)!");
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        if(!isLeaf(org))
        {
            List list = findChildOrganize(org);
            for(Iterator it = list.iterator(); it.hasNext();)
            {
                Organize organize = (Organize)it.next();
                if(!isRoot(organize))
                    addOrganizeNode(webRoot, element, organize, "course");
            }

        }
        return doc;
    }

    public void changeUrl(String oldUrl, String url)
        throws BusinessException
    {
        if(oldUrl == null || url == null)
            throw new BusinessException("null value isn't allowed in function changeUrl(String oldUrl, String url)!");
        List organize = null;
        String hql = "from Organize o where o.url like '" + oldUrl + "%'";
        try
        {
            organize = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't change uro by url:'" + url + "'!");
        }
        Organize org;
        for(Iterator it = organize.iterator(); it.hasNext(); updateOrganize(org))
        {
            org = (Organize)it.next();
            org.setUrl(org.getUrl().replaceFirst(oldUrl, url));
        }

    }

    public Organize addOrganize(Organize parentOrg, Organize org)
        throws BusinessException
    {
        if(org == null || parentOrg == null)
            throw new BusinessException("null value isn't allowed in function addOrganize(Organize parentOrg, Organize org)!");
        if(org.getType() >= parentOrg.getType())
        {
            org.setParentId(parentOrg.getId());
            org.setParentOrganize(parentOrg);
            org.setUrl(parentOrg.getUrl() + "/" + org.getName());
            org = createOrganize(org);
        } else
        {
            return null;
        }
        logger.info("\u65B0\u589E\u7EC4\u7EC7\u7ED3\u6784\u8282\u70B9:" + org.getName() + "\u6210\u529F!");
        return org;
    }

    public Group addGroup(Organize org, Group newGroup)
        throws BusinessException
    {
        if(org == null || newGroup == null)
            throw new BusinessException("null value isn't allowed in function addGroup(Organize org, Group newGroup)!");
        newGroup.setOrganize(org);
        newGroup.setOrganizeId(org.getId());
        try
        {
            newGroup = (Group)hbpservice.store(newGroup);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u7528\u6237\u7EC4\u9519\u8BEF:" + newGroup.getName());
        }
        return newGroup;
    }

    public User addUser(Organize org, User newUser)
        throws BusinessException
    {
        if(org == null || newUser == null)
            throw new BusinessException("null value isn't allowed in function addUser(Organize org, User newUser)!");
        newUser.setOrganizeId(org.getId());
        try
        {
            newUser = (User)hbpservice.store(newUser);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u7528\u6237\u53D1\u751F\u9519\u8BEF:" + newUser.getName());
        }
        return newUser;
    }

    public String getOrgCkBTree(Organize organize, String treeName)
        throws BusinessException
    {
        String str = addOrgCkBTreeRoot("", organize, treeName) + selectOrganize("", organize, treeName);
        return str;
    }

    private String addOrgCkBTreeRoot(String str, Organize organize, String treeName)
    {
        str = "var org" + organize.getId() + "=" + treeName + ".addChkNode(org_root,\"" + organize.getName() + "\",\"" + organize.getId() + "\");";
        return str;
    }

    private String selectOrganize(String str, Organize orga, String treeName)
        throws BusinessException
    {
        List list = null;
        try
        {
            list = findChildOrganize(orga);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230\u5B50\u8282\u70B9!");
        }
        for(Iterator it = list.iterator(); it.hasNext();)
        {
            Organize org = (Organize)it.next();
            if(!isRoot(org))
            {
                str = str + "var org" + org.getId() + "=" + treeName + ".addChkNode(org" + org.getParentId() + ",\"" + org.getName() + "\",\"" + org.getId() + "\");";
                str = selectOrganize(str, org, treeName);
            }
        }

        return str;
    }

    public boolean isChild(Organize org1, Organize org2)
        throws BusinessException
    {
        boolean flag = false;
        if(org1.getUrl().length() >= org2.getUrl().length() && org1.getUrl().substring(0, org2.getUrl().length()).equals(org2.getUrl()))
            flag = true;
        return flag;
    }

    public Document getUserDocument(Organize o)
        throws BusinessException
    {
        List organize = null;
        List user = null;
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        organize = findAllChildOrganize(o);
        for(Iterator orgIt = organize.iterator(); orgIt.hasNext();)
        {
            Organize org = (Organize)orgIt.next();
            user = findAllUser(org, 'a');
            Iterator userIt = user.iterator();
            Element orgElement = root.addElement(org.getName());
            while(userIt.hasNext()) 
            {
                User u = (User)userIt.next();
                if(u.getOrganizeId().equals(org.getId()))
                {
                    Element userElement = orgElement.addElement("user");
                    userElement.setText(u.getName() + "[" + u.getUserId() + "]");
                    userElement.addAttribute("id", u.getId());
                }
            }
        }

        return doc;
    }

    public void moveOrganize(String user_id, String organize_id)
        throws BusinessException
    {
        String sql = "update t_user set organize_id='" + organize_id + "' where id='" + user_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7528\u6237\u7EC4\u7EC7\u673A\u6784\u8C03\u52A8\u65F6\u51FA\u9519!");
        }
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.organize.OrganizeServiceImp.class.getName());
    }
}
